package pl.edu.agh.admin.core;

import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import pl.edu.agh.admin.api.CronFromatException;
import pl.edu.agh.admin.api.CronParameter;
import pl.edu.agh.admin.api.Task;
import pl.edu.agh.admin.api.User;

public class TaskMock implements Task {
	
	private User user;
	private String cron;
	private String command;
	private Map<CronParameter, String> params;
	
	public TaskMock(User user){
		//generate random task
		this.user = user;
		cron = "1 0 0 0 0";
		command = "echo";
		params = new HashMap<CronParameter, String>();
		params.put(CronParameter.HOME, "/home");
		params.put(CronParameter.SHELL, "/opt/bash");
	}
	
	public TaskMock(User user, String cron, String command, Map<CronParameter, String> params){
		this.user = user;
		this.cron = cron;
		this.command = command;
		this.params = params;
	}

	public void setParameters(Map<CronParameter, String> params){
		this.params = params;
	}
	
	@Override
	public Map<CronParameter, String> getParameters() {
		return params;
	}

	@Override
	public String getCommand() {
		return command;
	}

	@Override
	public String getPlainCron() {
		return cron;
	}

	@Override
	public List<Calendar> getAllMomentsOfExecution(Calendar until) {
		List<Calendar> list = new LinkedList<Calendar>();
		Random r = new Random(Calendar.getInstance().getTimeInMillis());
		int max = r.nextInt(10)+1;
		for(int i = 0;i<max;i++){
			int d = 5 + r.nextInt(3);
			int h = r.nextInt(60);
			int m = r.nextInt(60);
			Calendar cal = Calendar.getInstance();
			//Ultra Mega Final Pyton 88 was here
			//cal.set(2012, 4, d, h, m);
			cal.set(2012, 5, d, h, m);
			list.add(cal);
		}
		return list;
	}

	@Override
	public User getUser() {
		return user;
	}

	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Task){
			Task t = (Task)obj;
			return t.getCommand().equals(command) 
					&& t.getUser().equals(user) 
					&& t.getPlainCron().equals(cron) 
					&& t.getParameters().equals(params);
		}		
		return false;
	}

	@Override
	public int getNumberOfExecutions(Calendar until) {
		return 0;
	}

	@Override
	public Calendar getNextTimeOfExecution() {
		return Calendar.getInstance();
	}
	
	@Override
	public String getCron() 
	{
		return "1 4 8 8";
	}
}
